package com.helu.lubike.activity;

import android.Manifest;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

import com.helu.lubike.R;
import com.helu.lubike.fragment.MenuFragment;
import com.helu.lubike.utils.Constants;

public class MainActivity extends AppCompatActivity {

    private static final String MSG = "MainActivity";
    public DrawerLayout mDrawerLayout;
    private DrawerLayout.DrawerListener mDrawerToggle;
    private MenuFragment menuFragment;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        menuFragment = (MenuFragment) getSupportFragmentManager().findFragmentById(R.id.menu);

        initView();

        //获取粗略定位权限
        requestPermission(1, Manifest.permission.ACCESS_COARSE_LOCATION);

        //获取精确定位权限
        requestPermission(1, Manifest.permission.ACCESS_FINE_LOCATION);

    }

    private void initView() {
        mDrawerLayout = (DrawerLayout) findViewById(R.id.activity_main);


        mDrawerToggle = new DrawerLayout.DrawerListener() {

            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {

            }

            @Override
            public void onDrawerOpened(View drawerView) {

            }

            @Override
            public void onDrawerClosed(View drawerView) {

            }

            @Override
            public void onDrawerStateChanged(int newState) {

            }
        };

        mDrawerLayout.addDrawerListener(mDrawerToggle);

        //设置右面的侧滑菜单只能通过编程来打开
        mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, GravityCompat.END);
    }


    /**
     * 请求权限
     *
     * @param id         请求授权的id 唯一标识即可
     * @param permission 请求的权限
     */
    protected void requestPermission(int id, String permission) {
        //版本判断
        if (Build.VERSION.SDK_INT >= 23) {
            //减少是否拥有权限
            int checkCallPhonePermission = ContextCompat.checkSelfPermission(getApplicationContext(), permission);
            if (checkCallPhonePermission != PackageManager.PERMISSION_GRANTED) {
                //弹出对话框接收权限
                ActivityCompat.requestPermissions(MainActivity.this, new String[]{permission}, id);
            }
        }
    }


    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (data == null) {
            return;
        }

        if (ModifyUserNameActivity.RESULT_CODE == resultCode) {
            String phoneNumber = data.getStringExtra(Constants.PHONE_NUMBER);
            Log.d(MSG, "新手机号码是:" + phoneNumber);
            menuFragment.notifyPhoneNumber(phoneNumber);
        }
    }
}
